package com.zll.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zll.domain.User;
import com.zll.feign.UserService;
import com.zll.sentinel.OrderBlockHandler;
import com.zll.sentinel.OrderFallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUser/{id}")
    @SentinelResource(value = "order-getUser",
            blockHandler = "orderBlockHandler",
            blockHandlerClass = OrderBlockHandler.class,
            fallback = "OrderFallback",
            fallbackClass = OrderFallback.class)
    public User getUser(@PathVariable("id")Long id) {
        return userService.getUser(id);
    }

    // 必须加一个参数Throwable
    public User OrderFallback(@PathVariable("id")Long id,Throwable e) {
        return new User(-1L,"降级熔断","当前服务不可用，稍后重试！");
    }


    // 本类限流方法
/*public User orderBlockHandler(@PathVariable("id")Long id, BlockException e) {
        return new User(-1L,"你看你哪有好人样啊","整这死出！");
    }*/

}
